import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

// Custom APIs for renderer
const api = {}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
console.log('process.contextIsolated', process.contextIsolated)
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
    // 将自定义拖拽方法注册到electronAPI中
    contextBridge.exposeInMainWorld('customAPI',{
      /**
       * 发布main窗口操作消息
       * @param info{type: 操作类型, data: 数据}
       */
      publishMainWindowOperateMessage: (info: {event: string, data: {}}) => {
        ipcRenderer.send('Main_Window_Operate', info)
      }
    })
  } catch (error) {
    console.error(error)
  }
} else {
  // @ts-ignore (define in dts)
  // window.electron = electronAPI
  // // @ts-ignore (define in dts)
  // window.api = api
  // // @ts-ignore (define in dts)
  // window.customAPI = {
  //   /**
  //    * 发布main窗口操作消息
  //    * @param info{type: 操作类型, data: 数据}
  //    */
  //   publishMainWindowOperateMessage: (info: {event: string, data: {}}) => {
  //     ipcRenderer.send('Main_Window_Operate', info)
  //   }
  // }
}
